package test.math;
import java.security.MessageDigest;

/**
 * @descriptions md5数字签名算法
 * 
 * @date 2016年8月9日下午4:06:53
 * @author Yangcl
 * @version 1.0.1
 */
public class Md5SignUtil {

	public static void main(String[] args) {
		String s = md5Sign("3397@InterfaceA");
		//System.out.println(s);
		//{"head":{"target":"RESELL-SHOP-UPGRADE-RULE-AND-DATA-PROCESSOR","accessToken":"b185e50bd411161a7835bb82795ff25f","client":"3","version":"vsesion-2.0.0.1","requestTime":"2021-03-30 17:34:20","channel":"页面测试","key":"133C9CB27DA0","value":"1d59ba09198737e939dbbad683b4964f"},"data":{"cid":"2","resellerInfoId":362}}
		//{"head":{"target":"RESELLER-SHOP-INFO-DETAIL-PROCESSOR","accessToken":"b185e50bd411161a7835bb82795ff25f","client":"3","version":"vsesion-2.0.0.1","requestTime":"2021-03-30 17:34:20","channel":"页面测试","key":"133C9CB27DA0","value":"1d59ba09198737e939dbbad683b4964f"},"data":{"cid":"2","resellerInfoId":362}}

		//FD4007DB87B245EEACA7DAD5D4A1CECD 是通过 133C9CB27DA0 这个请求者的key得到的 value
		String s1 = Md5SignUtil.md5Sign("FD4007DB87B245EEACA7DAD5D4A1CECD" + "RESELLER-LOGIN-BY-MEMBER-TOKEN-PROCESS" + "2021-10-19 13:28:28");
		System.out.println(s1);
	}
	public static String md5Sign(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] btInput = s.getBytes("UTF-8");  // 解决MD5加密关于中文不一致问题 - Yangcl
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			mdInst.update(btInput);
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return "SignHelper.java exception";
		}
	}
}
